package com.qf.shixi.service.impl;

import com.qf.shixi.dao.MyDao;
import com.qf.shixi.entity.My;
import com.qf.shixi.exception.BusinessException;
import com.qf.shixi.result.ResponseCode;
import com.qf.shixi.service.MyService;
import com.qf.shixi.util.Md5Util;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (My)表服务实现类
 *
 * @author makejava
 * @since 2021-10-27 17:42:02
 */
@Service("myService")
public class MyServiceImpl implements MyService {
    @Resource
    private MyDao myDao;

    /**
     * 通过ID查询单条数据
     *
     * @param mId 主键
     * @return 实例对象
     */
    @Override
    public My queryById(Integer mId) {
        return this.myDao.queryById(mId);
    }

    //删了会报错
    @Override
    public Page<My> queryByPage(My my, PageRequest pageRequest) {
        return null;
    }


    /**
     * 新增数据
     *
     * @param my 实例对象
     * @return 实例对象
     */
    @Override
    public My insert(My my) {
        this.myDao.insert(my);
        return my;
    }

    /**
     * 修改数据
     *
     * @param  my 实例对象
     * @return 实例对象
     */


    /**
     * 通过主键删除数据
     *
     * @param mId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer mId) {
        return this.myDao.deleteById(mId) > 0;
    }

    /**
     * 修改数据
     *
     * @param my 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean update(My my) {
        My my1 = new My();
        my1.setToken(my.getToken());
        my1.setTel(my.getTel());
        List<My> mys = myDao.queryAllByConditions(my1);
        Integer update = 0;
        Integer insert = 0;
        if (mys.size() == 1) {
            my.setMId(mys.get(0).getMId());
            update = myDao.update(my);
            System.out.println(update + "-------");
        } else {
            insert = myDao.insert(my);
        }
        if (update > 0 || insert > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public My queryByToken(String token) {
        return myDao.queryByToken(token);
    }

    @Override
    public My myLogin(My my) {
        //通过个人id去数据库中查找用户
        My my1 = myDao.queryById(my.getMId());
        if (my1 == null) {
            throw new BusinessException(ResponseCode.AUTH_EXCEPTION.getCode(), ResponseCode.AUTH_EXCEPTION.getMessage());
        }
        String pwd = Md5Util.getMy(my.getMId(), my.getPassword());
        if (!my1.getPassword().equals(pwd)) {
            throw new BusinessException(ResponseCode.AUTH_EXCEPTION.getCode(), ResponseCode.AUTH_EXCEPTION.getMessage());
        }

        String token = UUID.randomUUID().toString();
        my1.setMyToken(token);
        myDao.update(my1);
//      将登陆成功的消息返回给前端
        my.setPassword("");
//        存放在redis
        my.setMyToken(token);
        return my;


    }

    /**
     * 修改tel数据
     *
     * @param my 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateTel(My my) {
        My my1 = new My();
        my1.setToken(my.getToken());
        List<My> mys = myDao.queryAllByConditions(my1);
        Integer update = 0;
        Integer insert = 0;
        if (mys.size() == 1) {
            my.setTel(mys.get(0).getTel());
            update = myDao.update(my);
            System.out.println(update + "-------");
        } else {
            insert = myDao.insert(my);
        }
        if (update > 0 || insert > 0) {
            return true;
        } else {
            return false;
        }
    }

}